package com.cssw.fyzb.busi.service.adnumprice;


import com.cssw.fyzb.core.dao.acct.GzAddPriceDao;
import com.cssw.fyzb.core.dao.adnumprice.AdNumPriceDao;
import com.cssw.fyzb.core.dao.user.UserExtDao;
import com.cssw.fyzb.core.model.dodb.*;
import com.cssw.fyzb.core.model.vo.adnumprice.AdNumPriceLogVo;
import com.cssw.fyzb.core.util.TimeUtil;
import com.ydn.dbframe.plugin.activerecord.Db;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author
 */
@Service
public class AdNumPriceServiceImpl implements AdNumPriceService {

    private static final Logger logger = LoggerFactory.getLogger(AdNumPriceServiceImpl.class);

    @Autowired
    private AdNumPriceDao adNumPriceDao;

    @Autowired
    private GzAddPriceDao gzAddPriceDao;

    @Autowired
    private UserExtDao userExtDao;

    @Override
    public AdNumPriceLogVo adNumPriceLog(Long userId){
        AdNumPriceLog adNumPriceLog = adNumPriceDao.getAdNumPriceLog(userId);
        AdNumPriceLogVo adNumPriceLogVo = new AdNumPriceLogVo();
        if (null != adNumPriceLog) {
            adNumPriceLogVo.setNum(adNumPriceLog.getNum());
            adNumPriceLogVo.setPrice(adNumPriceLog.getPrice());
        } else {
            adNumPriceLogVo.setNum(0);
            adNumPriceLogVo.setPrice(BigDecimal.ZERO);
        }
        return adNumPriceLogVo;
    }

    @Override
    public Boolean lookAd(Long userId){
        Db.tx(() -> {
            AdNumPriceLog numPriceLog = adNumPriceDao.getAdNumPriceLog(userId);
            if (null != numPriceLog) {
                AdNumPrice numPrice = adNumPriceDao.getConfig(numPriceLog.getNum() + 1);
                numPriceLog.setNum(numPriceLog.getNum() + 1);
                if (null != numPrice) {
                    numPriceLog.setPrice(numPrice.getPrice());
                    processGzAddPrice(userId, numPriceLog.getLogId(), numPrice);
                }
                adNumPriceDao.updateLog(numPriceLog);
            } else {
                AdNumPrice numPrice = adNumPriceDao.getConfig(1);
                if (null != numPrice) {
                    Long logId = adNumPriceDao.saveLog(userId, 1, numPrice.getPrice());
                    processGzAddPrice(userId, logId, numPrice);
                } else {
                    adNumPriceDao.saveLog(userId, 1, BigDecimal.ZERO);
                }
            }
            return true;
        });
        return true;
    }

    private void processGzAddPrice(Long userId, Long logId, AdNumPrice numPrice){
        GzAddPrice gzAddPrice = gzAddPriceDao.getTodayGzAddPrice(userId, GzAddPrice.TASK_TYPE_ADVERT);

        if (null == gzAddPrice) {
            userExtDao.addGzAddPrice(userId, numPrice.getPrice());
            gzAddPriceDao.save(userId, numPrice.getPrice(), GzAddPrice.TASK_TYPE_ADVERT, logId.toString(), "广告任务");
        } else {
            BigDecimal addPrice = numPrice.getPrice().subtract(gzAddPrice.getPrice());

            //如果没有增值价格，则不更新
            if (addPrice.compareTo(BigDecimal.ZERO) < 1) {
                return;
            }

            userExtDao.addGzAddPrice(userId, addPrice);
            gzAddPriceDao.updateGzAddPrice(gzAddPrice.getId(), numPrice.getPrice());
        }
    }
}
